<?php
class Mmessage extends CI_Model {
	function __construct() {
		parent::__construct ();
	}
	
	function addBusinessJoin($data) {
		$data ['state'] = 'F';
		$data ['creatTime'] = date ( "Y/m/d H:i:s", time () );
		$this->db->insert ( 'businessjoin', $data );
		if ($this->db->affected_rows () > 0)
			return TRUE;
		else
			return FALSE;
	}
	
	function addReservation($data) {
		$time = mktime ( date ( "H" ), date ( "i" ), date ( "s" ), date ( "m" ), date ( "d" ) + 20, date ( "Y" ) );
		$data ['state'] = 'F';
		$data ['creatTime'] = date ( "Y/m/d H:i:s", time () );
		$data ['reserveTime'] = date ( "Y/m/d H:i:s", $time );
		$this->db->insert ( 'reservation', $data );
		if ($this->db->affected_rows () > 0)
			return date ( "Y/m/d", $time );
		else
			return NULL;
	}
	
	function addMessage($data) {
		$data ['state'] = 'F';
		$data ['creatTime'] = date ( "Y/m/d H:i:s", time () );
		$this->db->insert ( 'message', $data );
		if ($this->db->affected_rows () > 0)
			return TRUE;
		else
			return FALSE;
	}
	
	function getBusinessJoinNum($mode, $search) {
		if ($search) {
			$this->db->where ( $search );
		}
		if ($mode == 1)
			$this->db->where ( 'state', 'T' );
		if ($mode == 2)
			$this->db->where ( 'state', 'F' );
		if ($mode == 3)
			$this->db->where ( 'state', 'D' );
		return $this->db->count_all_results ( 'businessjoin' );
	}
	
	function getReservationNum($mode, $search) {
		if ($search) {
			$this->db->where ( $search );
		}
		if ($mode == 1)
			$this->db->where ( 'state', 'T' );
		if ($mode == 2)
			$this->db->where ( 'state', 'F' );
		if ($mode == 3)
			$this->db->where ( 'state', 'D' );
		return $this->db->count_all_results ( 'reservation' );
	}
	
	function getMessageNum($mode, $search) {
		if ($search) {
			$this->db->where ( $search );
		}
		if ($mode == 1)
			$this->db->where ( 'state', 'T' );
		if ($mode == 2)
			$this->db->where ( 'state', 'F' );
		if ($mode == 3)
			$this->db->where ( 'state', 'D' );
		return $this->db->count_all_results ( 'message' );
	}
	
	function updateJoin($data) {
		$this->db->where ( 'id', $data ['id'] );
		$this->db->update ( 'businessjoin', $data );
		if ($this->db->affected_rows () > 0)
			return TRUE;
		else
			return FALSE;
	}
	
	function updateReservation($data) {
		$this->db->where ( 'id', $data ['id'] );
		$this->db->update ( 'reservation', $data );
		if ($this->db->affected_rows () > 0)
			return TRUE;
		else
			return FALSE;
	}
	
	function updateMessage($data) {
		$this->db->where ( 'id', $data ['id'] );
		$this->db->update ( 'message', $data );
		if ($this->db->affected_rows () > 0)
			return TRUE;
		else
			return FALSE;
	}
	
	function getBusinessJoinList($mode, $page, $num, $search) {
		if ($search) {
			$this->db->where ( $search );
		}
		if ($mode == 1)
			$this->db->where ( 'state', 'T' );
		if ($mode == 2)
			$this->db->where ( 'state', 'F' );
		if ($mode == 3)
			$this->db->where ( 'state', 'D' );
		$this->db->order_by ( "creatTime", "desc" );
		$this->db->limit ( $num, $page );
		$query = $this->db->get ( 'businessjoin' );
		return $query->result_array ();
	}
	
	function getReservationList($mode, $page, $num, $search) {
		if ($search) {
			$this->db->where ( $search );
		}
		if ($mode == 1)
			$this->db->where ( 'state', 'T' );
		if ($mode == 2)
			$this->db->where ( 'state', 'F' );
		if ($mode == 3)
			$this->db->where ( 'state', 'D' );
		$this->db->order_by ( "creatTime", "desc" );
		$this->db->limit ( $num, $page );
		$query = $this->db->get ( 'reservation' );
		return $query->result_array ();
	}
	
	function getMessageList($mode, $page, $num, $search) {
		if ($search) {
			$this->db->where ( $search );
		}
		if ($mode == 1)
			$this->db->where ( 'state', 'T' );
		if ($mode == 2)
			$this->db->where ( 'state', 'F' );
		if ($mode == 3)
			$this->db->where ( 'state', 'D' );
		$this->db->order_by ( "creatTime", "desc" );
		$this->db->limit ( $num, $page );
		$query = $this->db->get ( 'message' );
		return $query->result_array ();
	}
	
	function delJoinbyId($id) {
		$this->db->where ( 'id', $id );
		$this->db->delete ( 'businessjoin' );
		if ($this->db->affected_rows () > 0)
			return TRUE;
		else
			return FALSE;
	}
	
	function delReservationbyId($id) {
		$this->db->where ( 'id', $id );
		$this->db->delete ( 'reservation' );
		if ($this->db->affected_rows () > 0)
			return TRUE;
		else
			return FALSE;
	}
	
	function delMessagebyId($id) {
		$this->db->where ( 'id', $id );
		$this->db->delete ( 'message' );
		if ($this->db->affected_rows () > 0)
			return TRUE;
		else
			return FALSE;
	}
	
	function getJoinListforDownload($begin, $end) {
		$this->db->where ( "id >= $begin" );
		$this->db->where ( "id <= $end" );
		$query = $this->db->get ( 'businessjoin' );
		return $query->result_array ();
	}
	
	function getMessageListforDownload($begin, $end) {
		$this->db->where ( "id >= $begin" );
		$this->db->where ( "id <= $end" );
		$query = $this->db->get ( 'message' );
		return $query->result_array ();
	}
	
	function getReservationListforDownload($begin, $end) {
		$this->db->where ( "id >= $begin" );
		$this->db->where ( "id <= $end" );
		$query = $this->db->get ( 'reservation' );
		return $query->result_array ();
	}
	
	function getBusinessJoinMaxNum() {
		$this->db->select_max ( 'id', 'num' );
		$query = $this->db->get ( 'businessjoin' );
		$num = $query->row_array ();
		return $num ['num'];
	}
	
	function getMessageMaxNum() {
		$this->db->select_max ( 'id', 'num' );
		$query = $this->db->get ( 'message' );
		$num = $query->row_array ();
		return $num ['num'];
	}
	
	function getReservationMaxNum() {
		$this->db->select_max ( 'id', 'num' );
		$query = $this->db->get ( 'reservation' );
		$num = $query->row_array ();
		return $num ['num'];
	}
}